// Copyright Notice:
//				colortool.js v1.0.1
//			Copyright@2008 - OpenSiteMobile
//				All rights reserved
// ==========================================================================
//			http://www.opensitemobile.com
// ==========================================================================
// Contact Information:
//			Author: Dwight Vietzke
//			Email:  dwight_vietzke@yahoo.com

/*
       OpenSiteMobile colortool main object functions w/dojo integration
*/

dojo.provide("msos.colortool");
dojo.require("msos.common");
dojo.require("msos.validate");
dojo.require("dojo.i18n");


msos.colortool.generate_tool = function(element) {
    this.ct_color	= '000000';
    this.ct_path	= 'missing.html';
    this.ct_indicate_color = false;
    this.after_change_function = msos.colortool.close;
    this.colortool_window = null;
    this.colortool_button = null;

    // Get the language file to use as base
    dojo.requireLocalization('msos', 'colortool', djConfig.locale);
    var i18n_object = dojo.i18n.getLocalization('msos', 'colortool', djConfig.locale);

    // Configure colortool start button...
    this.ct_button_cfg = {
	img_name :      'ed_color.gif',
	img_url :       djConfig.msos_folder + '/images/',
	img_title :     i18n_object.click_title,
	img_alt :       i18n_object.click_alt,
	img_size :      'img_sml',
	button_mouseup : null
    };

    var but_obj = this;

    this.open_new_window = function() {
	if (msos.common.popup_cancel(but_obj.colortool_window)) {
	    if (djConfig.msos_popups_blocked) {
		alert(i18n_object.ct_disabled);
	    } else {
		window.name = 'color_select';
		but_obj.set_hex_color(element.value)
		var dial_arg = { ct_hex_inp : but_obj.ct_color };
		but_obj.colortool_window = msos.common.open_dialog_window(
		    but_obj,
		    but_obj.ct_path,
		    'colortool', 185, 305,      // this is as small as possible
		    but_obj.ct_display_color,
		    dial_arg
		);
		if (djConfig.msos_popups_blocked) alert(i18n_object.ct_blocked);
	    }
	}
    }

    this.ct_body_onfocus = function(evt) {
	msos.common.event_debug(evt, "\nColortool call page onfocus!");
	msos.common.popup_cancel(but_obj.colortool_window);
	return true;
    }

    this.update_onblur = function() {
	var previous = but_obj.ct_color;
	var current = msos.validate.check_hex_color(element.value);
	if (previous.toUpperCase() != current.toUpperCase()) {
	    but_obj.set_hex_color(current);
	    but_obj.after_change_function(but_obj);
	}
    }

    this.set_hex_color = function(hex) {
	hex = msos.validate.check_hex_color(hex);
	if	(hex == '000000')	{ element.value = '#000000';	but_obj.ct_color = '000000';	}
	else if	(hex == false)		{ element.value = '';		but_obj.ct_color = null;	}
	else				{ element.value = '#' + hex;	but_obj.ct_color = hex;		}
	if (but_obj.ct_indicate_color && but_obj.ct_color && but_obj.colortool_button) {
	    but_obj.colortool_button.img_elm.style.backgroundColor = '#' + but_obj.ct_color;
	}
    }

    this.ct_generate_button = function() {
	dojo.connect(element,	 'ondblclick',	null,	but_obj.open_new_window);
	dojo.connect(element,	 'onblur',	null,	but_obj.update_onblur);
	dojo.connect(dojo.body(),'onfocus',	null,	but_obj.ct_body_onfocus);

	var but_conf = but_obj.ct_button_cfg;
	but_conf.button_mouseup = but_obj.open_new_window;

	but_obj.set_hex_color(element.value);
	var ct_button = new msos.common.generate_img_button(element.parentNode);

	for (var val in but_conf) { ct_button[val] = but_conf[val]; }
	ct_button.img_button_append();

	// Set image button and input element info into the current ct button obj
	but_obj.colortool_button = ct_button;
	but_obj.colortool_input = element;

	// Store the whole mess in the original input element
	element.ct_object = but_obj;

	// Set bg color of colortool button to the current input value
	if (but_obj.ct_indicate_color) {
	      ct_button.img_elm.style.backgroundColor = element.value;
	}
	console.debug('ct_generate_button -> finished');
    }

    this.ct_display_color = function(out_obj) {
	if (!out_obj.ct_hex_inp) return false;
	but_obj.set_hex_color(out_obj.ct_hex_inp);
	but_obj.after_change_function(but_obj);
	return true;
    }
}

// Create an extended version of this default function to add more specific 'on_change' events
msos.colortool.close = function (ct_object) {
    msos.common.popup_cancel(ct_object.colortool_window);
    if (ct_object.ct_indicate_color && ct_object.ct_color && ct_object.colortool_button) {
       ct_object.colortool_button.img_elm.style.backgroundColor = '#' + ct_object.ct_color;
    }
    ct_object.colortool_input.focus();
}